package com.genesis.yunnanji.adapter;

import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.genesis.yunnanji.Constants;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.GoodsAttrBean;
import com.genesis.yunnanji.bean.GoodsAttrChildBean;
import com.genesis.yunnanji.bean.GoodsInfoBean;
import com.genesis.yunnanji.bean.RepertoryNumBean;
import com.genesis.yunnanji.view.GoodsDetailsActivity;
import com.google.android.flexbox.FlexboxLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * 创建日期：2018/10/12 001215:29
 * 作者：Kite
 * 描述：选择商品弹窗
 */
public class DialogFragmentGoodsSizeAdapter extends BaseQuickAdapter<GoodsAttrBean, BaseViewHolder> {

    private LayoutInflater inflater;

    private GoodsAttrChildBean colorSelectItem;
    private GoodsAttrChildBean sizeSelectItem;
    private GoodsDetailsActivity activity;

    public void bindActivity(GoodsDetailsActivity activity) {
        this.activity = activity;
    }

    public DialogFragmentGoodsSizeAdapter(int layoutResId, @Nullable List<GoodsAttrBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsAttrBean item) {
        if (inflater == null) {
            inflater = LayoutInflater.from(mContext);
        }
        helper.setText(R.id.tv_type, item.getType());
        FlexboxLayout flexboxLayout = helper.getView(R.id.flexLayout);
        flexboxLayout.removeAllViews();
        for (GoodsAttrChildBean s :
                item.getList()) {
            if (item.getType().equals("型号")) {
                createColorChildAttr(flexboxLayout, s);
            } else {
                createSizeChildAttr(flexboxLayout, s);
            }
        }
    }


    private void createColorChildAttr(FlexboxLayout flexboxLayout, final GoodsAttrChildBean item) {
        final ConstraintLayout itemView = (ConstraintLayout) inflater.inflate(R.layout.item_goods_size, getRecyclerView(), false);
        flexboxLayout.addView(itemView);
        TextView textView = (TextView) itemView.getChildAt(0);
        textView.setText(item.getName());
        if (item.isSelect()) {
            textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorTheme));
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_fff));
        } else {
            textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_f5));
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_000));
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (colorSelectItem == item) {
                    return;
                }
                if (colorSelectItem != null) {
                    colorSelectItem.setSelect(false);
                }
                item.setSelect(true);
                colorSelectItem = item;
                notifyDataSetChanged();
                String picUrl = "";
                for (GoodsInfoBean.PictureBean pic :
                        activity.goodsInfoBean.getPicture()) {
                    if (pic.getColor_id().equals(colorSelectItem.getId())) {
                        picUrl = pic.getCover_img();
                    }
                }
                if (!picUrl.equals("")) {
                    Glide.with(activity)
                            .load(Constants.PIC_ADMIN + picUrl)
                            .into(activity.dialogImage);
                }
                if (sizeSelectItem != null) {
                    for (GoodsInfoBean.StockBean s :
                            activity.goodsInfoBean.getStock()) {
                        if (s.getColor_id().equals(colorSelectItem.getId()) && s.getSize_id().equals(sizeSelectItem.getId())) {
                            activity.dialogPrice.setText(s.getSale_price());
                            activity.dialogStock.setText(s.getNums());
                            EventBus.getDefault().post(new RepertoryNumBean(s.getId(),s.getNums()));
                            break;
                        }
                    }
                }
            }
        });
    }

    private void createSizeChildAttr(FlexboxLayout flexboxLayout, final GoodsAttrChildBean item) {
        final ConstraintLayout itemView = (ConstraintLayout) inflater.inflate(R.layout.item_goods_size, getRecyclerView(), false);
        flexboxLayout.addView(itemView);
        TextView textView = (TextView) itemView.getChildAt(0);
        textView.setText(item.getName());
        if (item.isSelect()) {
            textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorTheme));
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_fff));
        } else {
            textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_f5));
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_000));
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sizeSelectItem == item) {
                    return;
                }
                if (sizeSelectItem != null) {
                    sizeSelectItem.setSelect(false);
                }
                item.setSelect(true);
                sizeSelectItem = item;
                notifyDataSetChanged();
                if (colorSelectItem != null) {
                    for (GoodsInfoBean.StockBean s :
                            activity.goodsInfoBean.getStock()) {
                        if (s.getColor_id().equals(colorSelectItem.getId()) && s.getSize_id().equals(sizeSelectItem.getId())) {
                            activity.dialogPrice.setText(s.getSale_price());
                            activity.dialogStock.setText(s.getNums());
                            EventBus.getDefault().post(new RepertoryNumBean(s.getId(),s.getNums()));
                            break;
                        }
                    }
                }
            }
        });
    }

}
